package com.fiume.travel.web;

import com.fiume.travel.enums.StatusEnum;
import com.fiume.travel.exception.ProjectException;
import com.fiume.travel.res.ResponseWrap;
import com.fiume.travel.service.FavoriteService;
import com.fiume.travel.utils.ExceptionsUtil;
import com.fiume.travel.vo.FavoriteVo;
import com.fiume.travel.vo.RouteVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * Description: 收藏模块
 * </p>
 *
 * @author fiume
 * @version v1.0.0
 * @create 2021-02-23 15:49
 * @see com.fiume.travel.web
 */
@RestController
@Log4j2
@CrossOrigin
@Api(tags = "收藏模块")
public class FavoriteController {

    @Resource
    FavoriteService favoriteService;

    @Value("${upLoad.webSite}")
    String webSite;

    /***
     * 添加收藏
     * @param favoriteVo 请求参数
     * @return java.lang.Integer
     */
    @PostMapping("favorite")
    public ResponseWrap<Integer> addFavorite(@RequestBody FavoriteVo favoriteVo) throws ProjectException {
        try {
            Integer flag = favoriteService.addFavorite(favoriteVo);
            System.out.println("flag = " + flag);
            return ResponseWrap.<Integer>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("添加收藏：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ADD_FAVORITE_FAIL.getCode(),
                    StatusEnum.ADD_FAVORITE_FAIL.getMsg());
        }

    }

    /***
     * 查询我的分页 需要注意的是：此处使用@RequestBody必须使用@PostMapping
     * @param favoriteVo 查询对象
     * @param pageNum 页码
     * @param pageSize 每页返回数
     * @return com.github.pagehelper.PageInfo<com.itheima.travel.req.RouteVo>
     */
    @PostMapping("favorite/{pageNum}/{pageSize}")
    public ResponseWrap<PageInfo<RouteVo>> findMyFavorite(@RequestBody FavoriteVo favoriteVo,
                                                          @PathVariable("pageNum")Integer pageNum,
                                                          @PathVariable("pageSize")Integer pageSize) throws ProjectException {
        try {
            PageInfo<RouteVo> myFavorite = favoriteService.findMyFavorite(favoriteVo, pageNum, pageSize);
            return ResponseWrap.<PageInfo<RouteVo>>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .webSite(webSite)
                    .data(myFavorite)
                    .build();
        }catch (Exception e){
            log.error("分页查询我的收藏：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.FIND_MYFAVORITE_FAIL.getCode(),
                    StatusEnum.FIND_MYFAVORITE_FAIL.getMsg());
        }

    }

    /***
     * 是否关注
     * @param routeId 线路Id
     * @return java.lang.Boolean
     */
    @GetMapping("favorite/{routeId}")
    public ResponseWrap<Boolean> isFavorited(@PathVariable("routeId") Long routeId) throws ProjectException {
        FavoriteVo favoriteVo = FavoriteVo.builder().routeId(routeId).build();
        try {
            Boolean flag = favoriteService.isFavorited(favoriteVo);
            return ResponseWrap.<Boolean>builder()
                    .code(StatusEnum.SUCCEED.getCode())
                    .msg(StatusEnum.SUCCEED.getMsg())
                    .operationTime(new Date())
                    .data(flag)
                    .build();
        }catch (Exception e){
            log.error("是否收藏：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(StatusEnum.ISFAVORITED_FAIL.getCode(),
                    StatusEnum.ISFAVORITED_FAIL.getMsg());
        }

    }
}
